# coding:utf-8
"""
description:1|2|3-gang switch US|UK，Dimmer switch US|UK
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML
from pages.Dashboard_page import dashboard_page

Switch_Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'GangsDimmer.yaml'))


class Switches_page(dashboard_page):
    by = mobileby.MobileBy()

    """    info: 0,setting page》Two Way Switch，Select Room弹框elements见Public.yaml（18，19）
    info2: 当选择成功后》取消的弹框elements见Public.yaml（0，1）"""
    TwoWaySwitch_loc = (by.ID, Switch_Data.get_elementInfo(0))

    def get_TwoWaySwitch(self):
        """点击2 way switch Connected开关按钮"""
        return self.find_element(*self.TwoWaySwitch_loc)

    def click_TwoWaySwitch(self):
        """    info: 0,setting page》Two Way Switch，Select Room弹框elements见Public.yaml（18，19）
        info2: 当选择成功后》取消的弹框elements见Public.yaml（0，1）"""
        self.find_element(*self.TwoWaySwitch_loc).click()

    # setting page》Two Way Switch，选择Location弹框elements见Public.yaml（3，18，19）
    WaySwitchLocation_loc = (by.ID, Switch_Data.get_elementInfo(1))

    def getText_WaySwitchLocation(self):
        """setting page》Two Way Switch>返回Location"""
        return self.find_element(*self.WaySwitchLocation_loc).text

    def click_WaySwitchLocation(self):
        """setting page》Two Way Switch，点击选择Location弹框elements见Public.yaml（3，18，19）"""
        self.find_element(*self.WaySwitchLocation_loc).click()

    # setting page》Two Way Switch，选择SwitchName弹框elements见Public.yaml（3，18，19）
    WaySwitchName_loc = (by.ID, Switch_Data.get_elementInfo(2))

    def getText_WaySwitchName(self):
        """setting page》Two Way Switch>返回Way switche name"""
        return self.find_element(*self.WaySwitchName_loc).text

    def click_WaySwitchName(self):
        """setting page》Two Way Switch，点击选择SwitchName弹框elements见Public.yaml（3，18，19）"""
        self.find_element(*self.WaySwitchName_loc).click()

    # setting page》Two Way Switch，选择Location选项弹框close按钮见Public.yaml（3）
    WaySwitchDeviceName_loc = (by.ID, Switch_Data.get_elementInfo(3))

    def get_WaySwitchDeviceName(self):
        """setting page》Two Way Switch>返回Way switche name"""
        return self.find_elements(*self.WaySwitchDeviceName_loc)

    # setting page》Two Way Switch，获取Device floor and room文字
    WaySwitchDeviceRoom_loc = (by.ID, Switch_Data.get_elementInfo(4))

    def get_WaySwitchDeviceRoom(self):
        """setting page》Two Way Switch，获取Device floor and room文字"""
        return self.find_elements(*self.WaySwitchDeviceRoom_loc)

    def func_2WaySwitch(self, Message):
        """
        open or CLose 2 way switch功能
        :param Message: 提示语内容
        """
        gesture_mainpulation().swipe_up(self.driver, self.get_DeviceSettingsPage())  # 滑动Setting page
        if self.assertVisible(*self.TwoWaySwitch_loc):
            if self.get_TwoWaySwitch().get_attribute("checked") == 'false':
                self.click_TwoWaySwitch()
                random.choice(super().get_availableRoomName()).click()  # 随机选择Location
                random.choice(self.get_WaySwitchDeviceRoom()).click()  # 随机选择Switch Name
                return super().find_toast(Message)
            else:
                # close 2 way switch功能
                self.click_TwoWaySwitch()
                super().click_confirm()  # 点击Remove 按钮
                return super().find_toast(Message)
        else:
            LOG.info("未找到open or CLose 2 way switch功能，请检查是否是1 Gang switch...")
            return True
